{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day02 初识元字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　在构建正则表达式时，绝大部分单个字符都可以用来匹配其本身，譬如下面几个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:50.790944Z",
     "iopub.status.busy": "2020-08-25T10:20:50.789946Z",
     "iopub.status.idle": "2020-08-25T10:20:50.806903Z",
     "shell.execute_reply": "2020-08-25T10:20:50.805906Z",
     "shell.execute_reply.started": "2020-08-25T10:20:50.790944Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['b', 'b', 'b', 'b', 'b', 'b']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "s = 'aaabbbbbbaaa'\n",
    "\n",
    "# 匹配1个b的模式并提取出来\n",
    "re.findall('b', s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:51.386351Z",
     "iopub.status.busy": "2020-08-25T10:20:51.386351Z",
     "iopub.status.idle": "2020-08-25T10:20:51.393332Z",
     "shell.execute_reply": "2020-08-25T10:20:51.392335Z",
     "shell.execute_reply.started": "2020-08-25T10:20:51.386351Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bb', 'bb', 'bb']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匹配2个连续b的模式并提取出来\n",
    "re.findall('bb', s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:51.801240Z",
     "iopub.status.busy": "2020-08-25T10:20:51.801240Z",
     "iopub.status.idle": "2020-08-25T10:20:51.809218Z",
     "shell.execute_reply": "2020-08-25T10:20:51.808223Z",
     "shell.execute_reply.started": "2020-08-25T10:20:51.801240Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bbb', 'bbb']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匹配3个连续b的模式并提取出来\n",
    "re.findall('bbb', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　这是最简单的正则表达式，即我们的正则表达式中写的是什么内容，就**直截了当**地匹配对应的字符串片段，但正则表达式为了实现更通用的字符模式匹配，衍生出了特定的一类特殊的**单个**字符，我们称之为**元字符**（*metacharacters* ），这些元字符在正则表达式中起着**特殊**的作用，因此直接用其来匹配它们自己是无效的，譬如`'.'`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:53.842776Z",
     "iopub.status.busy": "2020-08-25T10:20:53.842776Z",
     "iopub.status.idle": "2020-08-25T10:20:53.849758Z",
     "shell.execute_reply": "2020-08-25T10:20:53.848760Z",
     "shell.execute_reply.started": "2020-08-25T10:20:53.842776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'a', 'a', '.', '.', '.', 'a', 'a', 'a']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'aaa...aaa'\n",
    "\n",
    "# 直接传入.来匹配自身是无效的\n",
    "re.findall('.', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　可以看到上述的匹配结果并没有只把所有的`'.'`提取出来，对于**元字符**的匹配，我们需要在其之前添加`'\\'`来对其进行转义，才能匹配它本身代表的字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:55.951133Z",
     "iopub.status.busy": "2020-08-25T10:20:55.951133Z",
     "iopub.status.idle": "2020-08-25T10:20:55.958114Z",
     "shell.execute_reply": "2020-08-25T10:20:55.957117Z",
     "shell.execute_reply.started": "2020-08-25T10:20:55.951133Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.', '.', '.']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.findall('\\.', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　正则表达式中全部的**元字符**如下，关于他们在正则表达式中各自的特殊作用，我们将在之后的日程中为大家详细介绍：\n",
    " \n",
    "> . ^ $ * + ? { } [ ] \\ | ( )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Day02 课后小测验\n",
    "\n",
    "　　相信通过上面的内容，你已经对什么是**元字符**有了基本的认识，下面请你从下面的目标字符串`target`中提取所有的`'${|'`片段："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-25T10:20:58.504300Z",
     "iopub.status.busy": "2020-08-25T10:20:58.503302Z",
     "iopub.status.idle": "2020-08-25T10:20:58.509287Z",
     "shell.execute_reply": "2020-08-25T10:20:58.508291Z",
     "shell.execute_reply.started": "2020-08-25T10:20:58.504300Z"
    }
   },
   "outputs": [],
   "source": [
    "target = 'abcd${|ABC${|D.3213${|\\dsaddas'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 请把你的答案回复在【Day02】对应的帖子下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['${|', '${|', '${|']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.findall('\\$\\{\\|',target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
